import { defineStore } from 'pinia';
import { loginApi } from '@/api/user';

interface LoginParams {
  username: string;
  password: string;
}

//选项式写法
export const useUserStore = defineStore('user', {
  state: () => ({
    token: sessionStorage.getItem('token') || '',
    roles: sessionStorage.getItem('roles')
      ? JSON.parse(sessionStorage.getItem('roles')!)
      : [],
    username: sessionStorage.getItem('username') || '',
    menu: sessionStorage.getItem('menu')
      ? JSON.parse(sessionStorage.getItem('menu')!)
      : [],
  }),
  actions: {
    async login(data: LoginParams) {
      try {
        const res = await loginApi(data);
        const {
          token,
          user: { username, roles },
          menulist,
        } = res.data;
        this.token = token;
        console.log('🚀 ~ login ~ this.token:', this.token);
        this.roles = roles;
        this.username = username;
        this.menu = menulist;
        sessionStorage.setItem('token', token);
        sessionStorage.setItem('roles', JSON.stringify(roles));
        sessionStorage.setItem('username', username);
        sessionStorage.setItem('menu', JSON.stringify(menulist));
      } catch (err) {
        console.log('🚀 ~ login ~ err:', err);
      }
    },

    logout() {
      this.token = '';
      this.roles = [];
      this.username = '';
      this.menu = [];
      sessionStorage.clear()
    },
  },
});
